package com.moveme.examples.mouse;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.util.Timer;
import java.util.TimerTask;

import com.moveme.MoveMeClient;
import com.moveme.MoveMeConstants;
import com.moveme.MoveMeListener;
import com.moveme.network.payload.PSPayload;
import com.moveme.network.payload.state.PSMoveGemState;
import com.moveme.network.payload.state.PSMovePadData;
import com.moveme.network.payload.state.PSMoveState;

public class MouseControl implements MoveMeListener {

    /**
     * Class that controls the mouse.
     */
    private Robot robot;

    /**
     * Move me client.
     */
    private MoveMeClient moveMeClient;

    /**
     * Control selected (0 by default). There is only one mouse in almost all
     * computers.
     */
    private int moveControllerSelected = 0;

    /**
     * Move me server.
     */
    private static final String MOVE_ME_SERVER = "192.168.0.100";

    private int width;

    private int height;

    /**
     * True if the T button is pressed. False if not.
     */
    private boolean tButtonPressed = false;
    
    /**
     * True if the Move button is pressed. False if not.
     */
    private boolean moveButtonPressed = false;    

    /**
     * Execute some commands in the future.
     */
    private Timer timer;

    /**
     * Constructor.
     * 
     * At start up the program will wait 10 seconds and try to calibrate the
     * controller 0. So, it's needed to be ready after 10 seconds.
     */
    public MouseControl() {
        try {
            this.robot = new Robot();
            this.timer = new Timer();
            this.moveMeClient = new MoveMeClient(MOVE_ME_SERVER, 7899, 5566,
                    this);
            this.moveMeClient.init();
            this.moveMeClient.turnonPosition(moveControllerSelected);
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    moveMeClient.calibrate(moveControllerSelected);
                    moveMeClient.setSphereTrackcolor(4 << 24, 4 << 24, 4 << 24,
                            4 << 24);
                }
            }, 5000);
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension screenSize = toolkit.getScreenSize();
            this.width = screenSize.width;
            this.height = screenSize.height;
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MouseControl moveToMouse = new MouseControl();
    }

    @Override
    public void notifyError(Throwable t) {
        System.err.println(t);
    }

    @Override
    public void processData(PSPayload payload) {
        PSMoveState moveState = (PSMoveState) payload;
        /**
         * Get the normalized data.
         */
        float normalizedX = moveState.movePositionPointerState[moveControllerSelected].normalizedX;
        float normalizedY = moveState.movePositionPointerState[moveControllerSelected].normalizedY;
        int pointerX = (int) (normalizedX * this.width / 2) + this.width / 2;
        int pointerY = (int) ((-1) * normalizedY * this.height / 2)
                + this.height / 2;
        System.out.println(pointerX + ":" + pointerY);
        //this.robot.mouseMove(pointerX, pointerY);
        PSMovePadData padState = moveState.moveGemState[moveControllerSelected].pad;
        if (!tButtonPressed && padState.isButtonPressed(MoveMeConstants.T_BUTTON)) {
            this.robot.mousePress(InputEvent.BUTTON2_MASK);
            tButtonPressed = true;
        }
        if (tButtonPressed && !padState.isButtonPressed(MoveMeConstants.T_BUTTON)) {
            this.robot.mouseRelease(InputEvent.BUTTON2_MASK);
            tButtonPressed = false;
        }
        
        if (!moveButtonPressed && padState.isButtonPressed(MoveMeConstants.MOVE_BUTTON)) {
            this.robot.mousePress(InputEvent.BUTTON1_MASK);
            moveButtonPressed = true;
        }
        if (moveButtonPressed && !padState.isButtonPressed(MoveMeConstants.MOVE_BUTTON)) {
            this.robot.mouseRelease(InputEvent.BUTTON1_MASK);
            moveButtonPressed = false;
        }
    }

    @Override
    public void processImage(Image imageData) {
        // TODO Auto-generated method stub

    }
}
